package com.yc.sakuralxm.uestcscanning.ocrscan

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.app.Instrumentation
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.os.Environment
import android.renderscript.ScriptGroup
import android.text.Editable
import android.text.InputFilter
import android.text.InputType
import android.text.TextUtils
import android.text.TextWatcher
import android.util.DisplayMetrics
import android.view.KeyEvent
import android.view.MotionEvent
import android.view.View
import android.view.View.OnClickListener
import android.view.Window
import android.view.WindowManager
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView

import com.yc.sakuralxm.uestcscanning.AppContext
import com.yc.sakuralxm.uestcscanning.BaseActivity
import com.yc.sakuralxm.uestcscanning.R
import com.yc.sakuralxm.uestcscanning.activity.CarInfoActivity
import com.yc.sakuralxm.uestcscanning.carnum.CarKeyboardUtil
import com.yc.sakuralxm.uestcscanning.tools.InputMethodUtil
import com.yc.sakuralxm.uestcscanning.tools.IntentUtil
import com.yc.sakuralxm.uestcscanning.tools.L
import com.yc.sakuralxm.uestcscanning.tools.NoDoubleClickListener
import com.yc.sakuralxm.uestcscanning.tools.ViewUtil

import java.util.ArrayList

class MemoryResultActivity : BaseActivity() {

    private var bitmap: Bitmap? = null
    private var bitmapPath: String? = null
    private var car_number: String? = null
    private var idEt1: EditText? = null
    private val idEt2: EditText? = null
    private val idEt3: EditText? = null
    private val idEt4: EditText? = null
    private val idEt5: EditText? = null
    private val idEt6: EditText? = null
    private val idEt7: EditText? = null
    private var imgCar: ImageView? = null

    private var keyboardUtil: CarKeyboardUtil? = null
    private val keyboardUtil2: CarKeyboardUtil? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        // TODO Auto-generated method stub      这里的直接跳转键盘无法输入  键盘没有用车牌专用键盘
        super.onCreate(savedInstanceState)

        requestWindowFeature(Window.FEATURE_NO_TITLE)// 隐藏标题
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN)// 设置全屏
        setContentView(R.layout.activity_result)
        findView()
        showInput(idEt1)
        //        keyboardUtil2 = new CarKeyboardUtil(this, idEt2);
        //        keyboardUtil2.hideSoftInputMethod();
        initData()
        BaseActivity.setRig(activity, "首页")
        //		recogType  = getIntent().getBooleanExtra("recogType", false);
        //        System.out.println("识别时间：" + getIntent().getStringExtra("time"));
    }

    private fun showInput(idEt100: EditText?) {
        //        if (keyboardUtil != null)
        //            keyboardUtil = null;
        keyboardUtil = CarKeyboardUtil(this, idEt100)
        keyboardUtil!!.hideSoftInputMethod()
        keyboardUtil!!.hideKeyboard()
        //        keyboardUtil.hideSystemKeyBroad();
    }

    fun dis(view: View) {

        if (keyboardUtil!!.isShow) {
            keyboardUtil!!.hideKeyboard()
        }
    }


    internal interface ICallBack {
        fun doAction(tv: TextView, et: EditText)

        fun ok(et: EditText)
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun initData() {
        title = "车牌识别"
        bitmapPath = intent.getStringExtra("path")
        if (!TextUtils.isEmpty(bitmapPath))
            bitmap = BitmapFactory.decodeFile(bitmapPath)
        car_number = intent.getCharSequenceExtra("number") as String
        if (bitmap != null)
            imgCar!!.setImageBitmap(bitmap)
        if (!TextUtils.isEmpty(car_number)) {
            //		for (int i = 0; i <car_number.length() ; i++) {
            idEt1?.let { ViewUtil.setTvText(it, car_number) }
        }

        //            if (car_number.length() > 1) {
        //                ViewUtil.setTvText(idEt2, car_number.substring(1, 2));
        //
        //            }
        //            if (car_number.length() > 2) {
        //                ViewUtil.setTvText(idEt3, car_number.substring(2, 3));
        //
        //            }
        //            if (car_number.length() > 3) {
        //                ViewUtil.setTvText(idEt4, car_number.substring(3, 4));
        //
        //            }
        //            if (car_number.length() > 4) {
        //                ViewUtil.setTvText(idEt5, car_number.substring(4, 5));
        //
        //            }
        //            if (car_number.length() > 5) {
        //                ViewUtil.setTvText(idEt6, car_number.substring(5, 6));
        //
        //            }
        //            if (car_number.length() > 6) {
        //                ViewUtil.setTvText(idEt7, car_number.substring(6, 7));
        //
        //            }
        //        }

        idEt1!!.setOnTouchListener { v, event ->
            if (!keyboardUtil!!.isShow) {
                keyboardUtil!!.showKeyboard()
            }
            false
        }

        //                    setOnClickListener(new NoDoubleClickListener() {
        //                @Override
        //                protected void onNoDoubleClick(View v) {
        //
        //                    L.e("点击了edt");
        ////                    showInput(idEt1);
        //                    if (!keyboardUtil.isShow()){
        //                        keyboardUtil.showKeyboard();
        //                    }
        //                }
        //            });
        val filters = arrayOf<InputFilter>(InputFilter.LengthFilter(7))
        idEt1!!.filters = filters
        idEt1!!.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable) {
                L.e(s.length.toString() + "")
                if (s.length == 7) {
                    if (keyboardUtil!!.isShow) {
                        keyboardUtil!!.hideKeyboard()
                    }
                }
            }
        })

    }

    /**
     * @param
     * @return void    返回类型
     * @throws
     * @Title: findView
     * @Description: TODO(这里用一句话描述这个方法的作用)
     */


    private fun findView() {
        // TODO Auto-generated method stub
        idEt1 = findViewById(R.id.idEt1) as EditText
        imgCar = findViewById(R.id.imgCar) as ImageView
        findViewById(R.id.idCheckCar).setOnClickListener {  goCar() }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (keyboardUtil!!.isShow) {
                keyboardUtil!!.hideKeyboard()
                return true
            }

        }

        return super.onKeyDown(keyCode, event)
    }

    override fun onDestroy() {
        super.onDestroy()

        if (bitmap != null) {
            bitmap = null
        }

        // 注意回收bitmap 不然兄弟你的内存堪忧
    }

    override fun onBackPressed() {
        Thread {hideInput() }.start()



        super.onBackPressed()
    }

    private fun hideInput() {
        val inst = Instrumentation()
        inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK)
    }

    //
    //    }

    private fun goCar() {
        val getCar = StringBuilder()
        getCar.append(idEt1!!.text)
        L.e("点击跳转")
        if (TextUtils.isEmpty(getCar) || getCar.length < 7) {
            AppContext.app.showToastCenterShort("请输入完成车牌")
            return

        }
        L.e("点击跳转2")

        val intent = Intent(this@MemoryResultActivity, CarInfoActivity::class.java)
        intent.putExtra("bitmapPath", bitmapPath)
        intent.putExtra("car_num", getCar.toString())
        startActivity(intent)
    }

    companion object {
        private val PATH = Environment
                .getExternalStorageDirectory().toString() + "/DCIM/Camera/"

        private fun showAlterDialog(activity: Activity, callBack: ICallBack) {
            val alertDialog = Dialog(activity, R.style.DialogTheme)
            alertDialog.show()
            val window = alertDialog.window
            window!!.setBackgroundDrawable(ColorDrawable(0))//设置背景透明
            window.setContentView(R.layout.dialog_alter)
            val idDialogAlterTitleTv = window.findViewById(R.id.idDialogAlterTitleTv) as TextView
            val idDialogAlterInputEt = window.findViewById(R.id.idDialogAlterInputEt) as EditText
            val idDialogAlterCancelTv = window.findViewById(R.id.idDialogAlterCancelTv) as TextView
            val idDialogAlterOkTv = window.findViewById(R.id.idDialogAlterOkTv) as TextView
            callBack.doAction(idDialogAlterTitleTv, idDialogAlterInputEt)
            InputMethodUtil.show(activity, idDialogAlterInputEt)
            ViewUtil.setEtActionClick(idDialogAlterInputEt, idDialogAlterOkTv)
            idDialogAlterCancelTv.text = "清除"
            idDialogAlterCancelTv.setOnClickListener { idDialogAlterInputEt.setText("") }
            idDialogAlterOkTv.setOnClickListener {
                alertDialog.dismiss()
                callBack.ok(idDialogAlterInputEt)
            }
        }
    }
}
